$(function () {
    var user_name = localStorage.getItem("user_name")
    var token = localStorage.getItem("token")
    if (user_name != null) {
        $("#login").append("<li role=\"presentation\"><a href=\"#\">管理用户:" + user_name + "</a></li><li role=\"presentation\"><a href=\"javascript:localStorage.clear();location.href ='/'\">退出</a></li>")
    } else {
        $("#login").append("<li role=\"presentation\"><a href=\"login\">用户登录</a></li>")
    }
    //检测登录状态
    if (token != null) {
        $.ajax({
            "url": "v1/user/check",
            type: "POST",
            headers: {token: localStorage.getItem("token")},
            success: function (data) {
                localStorage.setItem("user_name", data.users.user_name)
            },
            error: function (data) {
                if (data.responseJSON.msg) {
                    alert(data.responseJSON.msg)
                    localStorage.clear()
                    return false
                }
            }
        })
    }
    //获取网站栏目
    $.ajax({
        "url": "/v1/art/category",
        type: "GET",
        "dataType": "json",
        success: function (data) {
            var html = '';
            $.each(data.data, function (i, item) {
                html += "<li><a href=/?category_id=" + item.id + ">" + item.name + "(" + item.count + ")" + "<span class=\"sr-only\">(current)</span></a></li>\n"
            })
            $("#navbar-nav").append(html)
        }
    })

    //获取配置信息
    $.ajax({
        "url": "/info",
        type: "GET",
        "dataType": "json",
        success: function (data) {
            console.log(data)
            var html = '';
            html += " <li role=\"presentation\" class=\"active\"><a href=\"#\">本站数据统计</a></li><li role=\"presentation\"><a href=\"#\">采集数据仅用于学习研究,如侵权请联系微信13243840799删除！</a></li><li role=\"presentation\"><a href=\"#\">总数:" + data.count1 + "</a></li><li role=\"presentation\"><a href=\"#\">今日采集:" + data.count2 + "</a></li></li>"
            $("#example").append(html)
        }
    })

    var $_GET = (function () {
        var url = window.document.location.href.toString();
        var u = url.split("?");
        if (typeof (u[1]) == "string") {
            u = u[1].split("&");
            var get = {};
            for (var i in u) {
                var j = u[i].split("=");
                get[j[0]] = j[1];
            }
            return get;
        } else {
            return {};
        }
    })();
    var data = {}
    if ($_GET['category_id'] != undefined) {
        data = {"category_id": $_GET['category_id']}
    }
    if ($_GET['key'] != undefined) {
        data = {"key": decodeURIComponent($_GET['key'])}
    }

    //遍历文章
    $.ajax({
        "url": "/v1/art/list",
        type: "GET",
        "data": data,
        success: function (data) {
            var html = '';
            $.each(data.data, function (i, item) {
                var ht = ''
                if (item.url !== "") {
                    ht = "<a href=" + item.url + " target=_blank>来源</a>&nbsp;&nbsp;"
                }
                if (token && user_name) {
                    ht += "<a href='javascript:del(" + item.id + ")'>删除</a>"
                }
                html += "<div class=\"caption\"><h4><a href=\'detail/?id=" + item.id + "\'>" + item.title + "</h4></a><p style='float: right'>&nbsp; 栏目:" + item.category.name + " " + ht + "</p></div>"
            })
            $("#thumbnail").append(html)
            $("#pagination").append(data.page)

        }
    })
})
$("#logout").click(function (){
    localStorage.clear()
    location.href="/"
})
//删除
function del(id) {
    $.ajax({
        "url": "/v1/art/article_del",
        type: "GET",
        headers: {token: localStorage.getItem("token")},
        data: {"id":id},
        success: function (data) {
            location.href = "/"
        },
        error: function (data) {
            alert(data.responseJSON.msg)
        }
    })
}